﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class ScheduleDAO : CoreAbstractDAO<Schedule>
    {
        private AdapterAbstractDAO<Schedule, ScheduleSchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<Schedule, ScheduleUniversity> wrappedAbstractDAOUniversity;
        private ScheduleDAOSchool wrappedDAOSchool;
        private ScheduleDAOUniversity wrappedDAOUniversity;

        public ScheduleDAO(ScheduleDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool =
                new AdapterAbstractDAO<Schedule, ScheduleSchool>((AbstractDAO<ScheduleSchool>)wrappedDAOSchool);
        }

        public ScheduleDAO(ScheduleDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<Schedule, ScheduleUniversity>((AbstractDAO<ScheduleUniversity>)wrappedDAOUniversity);
        }

        public IList<Schedule> getByDay(Day day)
        {
            if (wrappedDAOSchool != null)
            {
                List<ScheduleSchool> list = (List<ScheduleSchool>)wrappedDAOSchool.getByDay(day);
                return list.ConvertAll(x => (Schedule)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<ScheduleUniversity> list = (List<ScheduleUniversity>)wrappedDAOUniversity.getByDay(day);
                return list.ConvertAll(x => (Schedule)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public IList<Schedule> getByClassroom(Classroom classroom)
        {
            if (wrappedDAOSchool != null)
            {
                List<ScheduleSchool> list = (List<ScheduleSchool>)wrappedDAOSchool.getByClassroom(classroom);
                return list.ConvertAll(x => (Schedule)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<ScheduleUniversity> list = (List<ScheduleUniversity>)wrappedDAOUniversity.getByClassroom(classroom);
                return list.ConvertAll(x => (Schedule)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public IList<Schedule> getByGroup(Group group)
        {
            if (wrappedDAOSchool != null)
            {
                List<ScheduleSchool> list = (List<ScheduleSchool>)wrappedDAOSchool.getByGroup(group);
                return list.ConvertAll(x => (Schedule)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                throw new NotSupportedMethodException("University doesn't support groups");
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public IList<Schedule> getByTeacher(Teacher teacher)
        {
            if (wrappedDAOSchool != null)
            {
                List<ScheduleSchool> list = (List<ScheduleSchool>)wrappedDAOSchool.getByTeacher(teacher);
                return list.ConvertAll(x => (Schedule)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<ScheduleUniversity> list = (List<ScheduleUniversity>)wrappedDAOUniversity.getByTeacher(teacher);
                return list.ConvertAll(x => (Schedule)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public void deleteAll()
        {
            if (wrappedDAOSchool != null)
            {
                wrappedDAOSchool.deleteAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedDAOUniversity.deleteAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public override void update(Schedule entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.update(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public override void save(Schedule entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(Schedule entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public override void delete(Schedule entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }
        }

        public override Schedule getById(int id)
        {
            Schedule entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<Schedule> getAll()
        {
            IList<Schedule> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ScheduleDAOSchool nor ScheduleDAOUniversity was initialized");
            }

            return records;
        }
    }
}
